import axios from 'axios';
import wrapper from 'axios-cache-plugin'

var isMobile = true; // h5 app 小程序 都使用token
export var token = '';

var base;
if (process.env.NODE_ENV == "development") { //开发环境
  base = 'http://192.168.0.105:8361'
  base = 'http://d7game.free.ngrok.cc'
  // base = 'http://thinkido.tunnel.qydev.com'
  // base='http://7dtime.tunnel.echomod.cn'
  // base = 'https://7dtime.com'
} else {
  base = 'https://7dtime.com' //发布环境
  //  base = 'http://d7game.free.ngrok.cc'
}
axios.defaults.withCredentials = true;
let http = wrapper(axios, {
  maxCacheSize: 15
})
http.__addFilter(/api\/z39/)
export const globalUrl = base;
//---------------系统通用接口---------------
// token登录
export const loginToken = params => {
  return axios.post(`${ base }/center/public/logintoken`, params).then(res => {
    let data = res.data;
    if (data.data && data.data.uid) {
      token = params.token;
      localStorage.setItem('token', token);
      // console.log("token:", token);
    }
    return Promise.resolve(data);
  });
};
// 登录
export const login = params => {
  return axios.post(`${ base }/center/public/loginmob`, params).then(res => {
    let data = res.data;
    if (data.data && data.data.token) {
      token = data.data.token;
      localStorage.setItem('token', token);
      // console.log("token:", token);
    }
    return Promise.resolve(data);
  });
};
// 退出登录
export const logout = params => {
  return axios.post(`${ base }/center/public/logoutmob`, params).then(res => {
    token = "";
    return Promise.resolve(res.data);
  });
};

// 注册
export const reg = params => {
  return axios.post(`${ base }/center/weixin/organizing`, params).then(res => {
    let data = res.data;
    if (data.data && data.data.token) {
      token = data.data.token;
      localStorage.setItem('token', token);
    }
    return Promise.resolve(data);
  });
  // return axios.post(`${ base }/center/weixin/organizing`, params).then(res => res.data);
};
// 获取手机注册验证码
export const mobCode = params => {
  return axios.post(`${ base }/ext/dayu/index/verifycodesend`, params).then(res => res.data);
};
// 修改密码
export const updatepwd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/center/seting/updatepassword`, params).then(res => res.data);
};
export const jssdk = params => {
  return axios.post(`${ base }/api/d7game/jssdk`, params).then(res => res.data);
};
export const authurl = params => {
  return axios.post(`${ base }/api/d7game/authurl`, params).then(res => res.data);
};
//  ------------------------- 建议 rest相关处理 -------------------------
export const suggestGet = params => {
  return http.get(`${ base }/api/d7suggest`, { "params": params }).then(res => res.data);
};
export const suggestAdd = params => {
  return axios.post(`${ base }/api/d7suggest`, params).then(res => res.data);
};
export const suggestSet = params => {
  return axios.put(`${ base }/api/d7suggest`, params).then(res => res.data);
};
export const suggestDel = params => {
  return axios.delete(`${ base }/api/d7suggest`, { "params": params }).then(res => res.data);
};
//  ------------------------- 合作 rest相关处理 -------------------------
export const cooperateGet = params => {
  return http.get(`${ base }/api/d7cooperate`, { "params": params }).then(res => res.data);
};
export const cooperateAdd = params => {
  return axios.post(`${ base }/api/d7cooperate`, params).then(res => res.data);
};
export const cooperateSet = params => {
  return axios.put(`${ base }/api/d7cooperate`, params).then(res => res.data);
};
export const cooperateDel = params => {
  return axios.delete(`${ base }/api/d7cooperate`, { "params": params }).then(res => res.data);
};
//---------------系统通用接口 结束---------------

export const info = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/apix/z43phone/info`, params).then(res => res.data);
};
export const phones = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/apix/z43phone/phones`, params).then(res => res.data);
};
export const call = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/apix/z43phone/call`, params).then(res => res.data);
};
export const getmp3 = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/apix/z43phone/getmp3`, params).then(res => res.data);
};
export const delinfo = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/apix/z43phone/delinfo`, params).then(res => res.data);
};
export const order = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/apix/z43phone/order`, params).then(res => res.data);
};
export const getuser = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/apix/z43phone/getuser`, params).then(res => res.data);
};

// 群查询
export const groups = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z43group`, { "params": params }).then(res => res.data);
};
// 群创建
export const groupAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z43group`, params).then(res => res.data);
};
// 群修改
export const groupSet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z43group`, params).then(res => res.data);
};
// 群删除
export const groupDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z43group`, { "params": params }).then(res => res.data);
};

// 群功能-短信
export const mobinfo = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z43mobinfo`, params).then(res => res.data);
};


// 成员查询
export const friends = params => { //和z43phone/phones 一样
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/apix/z39time/friends`, params).then(res => res.data);
};
// 成员查询
export const getProjectFriends = params => { //和z43phone/phones 一样
  return axios.get(`${ base }/api/z39friend`, { "params": params }).then(res => res.data);
};
// 成员创建
export const friendAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z43friend`, params).then(res => res.data);
};
// 成员修改
export const friendSet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z43friend`, params).then(res => res.data);
};
// 成员删除
export const friendDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z43friend`, { "params": params }).then(res => res.data);
};

// 群成员导入
export const friendAddGrp = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z43friends/addgrp`, params).then(res => res.data);
};
// 通讯录导入
export const friendsAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z43friends/add`, params).then(res => res.data);
};
// 群成员邀请
export const invitefriends = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/api/z39invite`, { "params": params }).then(res => res.data);
};
export const getInviteTitle = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/apix/z39time/getinvitetitle`, { "params": params }).then(res => res.data);
};
// 获取  app自动记录的任务
export const taskautoGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39taskauto`, { "params": params }).then(res => res.data);
};
export const taskautoAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39taskauto`, params).then(res => res.data);
};
export const taskautoPut = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39taskauto`, params).then(res => res.data);
};
export const taskautoDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39taskauto`, { "params": params }).then(res => res.data);
};


// 获取登录初始数据. 
export const usertask = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/apix/z39time/usertask`, { "params": params }).then(res => {
    token = token || localStorage.getItem('token');
    // console.log("token:", token);
    return Promise.resolve(res.data);
  });
};
//检查是否登陆
export const checkLogin = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/apix/z39base/userinfo`, { "params": params }).then(res => res.data);
};
export const days = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/apix/z39time/days`, { "params": params }).then(res => res.data);
};
export const sortdayGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/apix/z39time/sortday`, { "params": params }).then(res => res.data);
};
export const sortdayAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/apix/z39time/sortdayadd`, { "params": params }).then(res => res.data);
};
// 获取最近常用标签
export const tagsrec = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/apix/z39time/tagsrec`, { "params": params }).then(res => res.data);
};


//  ------------------------- 任务 rest相关处理 -------------------------
export const tasksGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39tasks`, { "params": params }).then(res => res.data);
};
export const tasksAdd = params => {
  var item;
  for (var i = params.tasks.length - 1; i >= 0; i--) {
    item = params.tasks[i];
    if (item.title && item.title.length > 100) return Promise.resolve({ errno: 1001, errmsg: "标题最长100字" });
    if (item.desc && item.desc.length > 9000) return Promise.resolve({ errno: 1001, errmsg: "描述最长9000字" });
  };
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39tasks`, params).then(res => res.data);
};
export const taskGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/api/z39task`, { "params": params }).then(res => res.data);
};
export const taskAdd = params => {
  if (params.title && params.title.length > 100) return Promise.resolve({ errno: 1001, errmsg: "标题最长100字" });
  if (params.desc && params.desc.length > 9000) return Promise.resolve({ errno: 1001, errmsg: "描述最长9000字" });
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39task`, params).then(res => res.data);
};
export const taskSet = params => {
  if (params.title && params.title.length > 100) return Promise.resolve({ errno: 1001, errmsg: "标题最长100字" });
  if (params.desc && params.desc.length > 9000) return Promise.resolve({ errno: 1001, errmsg: "描述最长9000字" });
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39task`, params).then(res => res.data);
};
export const taskDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39task`, { "params": params }).then(res => res.data);
};
export const taskMend = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/apix/z39time/taskmend`, { "params": params }).then(res => res.data);
};

//  ------------------------- 项目 rest相关处理 -------------------------
export const projects = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39projects`, { "params": params }).then(res => res.data);
};
export const projectGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/api/z39project`, { "params": params }).then(res => res.data);
};
export const projectAdd = params => {
  if (params.title && params.title.length > 15) return Promise.resolve({ errno: 1001, errmsg: "标题最长15字" });
  if (params.desc && params.desc.length > 100) return Promise.resolve({ errno: 1001, errmsg: "描述最长100字" });
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39project`, params).then(res => res.data);
};
export const projectSet = params => {
  if (params.title && params.title.length > 15) return Promise.resolve({ errno: 1001, errmsg: "标题最长15字" });
  if (params.desc && params.desc.length > 100) return Promise.resolve({ errno: 1001, errmsg: "描述最长100字" });
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39project`, params).then(res => res.data);
};
export const projectDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39project`, { "params": params }).then(res => res.data);
};

//  ------------------------- 标签 rest相关处理 -------------------------
export const tagGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39tag`, { "params": params }).then(res => res.data);
};
export const tagAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39tag`, params).then(res => res.data);
};
export const tagSet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39tag`, params).then(res => res.data);
};
export const tagDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39tag`, { "params": params }).then(res => res.data);
};

export const tagsAdd = params => {
  return axios.post(`${ base }/api/z39tags`, params).then(res => res.data);
};

//  ------------------------- 标签模板 rest相关处理 -------------------------
export const tagTmpAdd = params => {
  return axios.post(`${ base }/api/z39tagtmp`, params).then(res => res.data);
};

export const tagTmpPut = params => {
  return axios.put(`${ base }/api/z39tagtmp`, params).then(res => res.data);
};
export const tagTmpGet = params => {
  return axios.get(`${ base }/api/z39tagtmp`, { "params": params }).then(res => res.data);
};
export const tagTmpDel = params => {
  return axios.delete(`${ base }/api/z39tagtmp`, { "params": params }).then(res => res.data);
};
//  ------------------------- 日报文章 rest相关处理 -------------------------
export const blogs = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/api/z39blogs`, { "params": params }).then(res => res.data);
};
export const blogGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/api/z39blog`, { "params": params }).then(res => res.data);
};
export const blogAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39blog`, params).then(res => res.data);
};
export const blogSet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39blog`, params).then(res => res.data);
};
export const blogDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39blog`, { "params": params }).then(res => res.data);
};

//  ------------------------- 项目成员 rest相关处理 -------------------------
export const members = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39members`, { "params": params }).then(res => res.data);
};
export const memberGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.get(`${ base }/api/z39member`, { "params": params }).then(res => res.data);
};
export const memberAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39member`, params).then(res => res.data);
};
export const memberSet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39member`, params).then(res => res.data);
};
export const memberDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39member`, { "params": params }).then(res => res.data);
};

//  ------------------------- 重复任务 rest相关处理 -------------------------
export const repeatGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39repeat`, { "params": params }).then(res => res.data);
};
export const repeatAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39repeat`, params).then(res => res.data);
};
export const repeatSet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39repeat`, params).then(res => res.data);
};
export const repeatDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39repeat`, { "params": params }).then(res => res.data);
};

//  ------------------------- 标签 rest相关处理 -------------------------
export const tagitemsGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39tagitem`, { "params": params }).then(res => res.data);
};
export const tagitemsAdd = params => {
  return axios.post(`${ base }/api/z39tagitem`, params).then(res => res.data);
};

export const tagitemGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39tagitem`, { "params": params }).then(res => res.data);
};
export const tagitemAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39tagitem`, params).then(res => res.data);
};
export const tagitemSet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39tagitem`, params).then(res => res.data);
};
export const tagitemDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39tagitem`, { "params": params }).then(res => res.data);
};
//  ------------------------- 任务音频录音 rest相关处理 -------------------------
export const audioGet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return http.get(`${ base }/api/z39audio`, { "params": params }).then(res => res.data);
};
export const audioAdd = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.post(`${ base }/api/z39audio`, params).then(res => res.data);
};
export const audioSet = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.put(`${ base }/api/z39audio`, params).then(res => res.data);
};
export const audioDel = params => {
  isMobile && token && (params = params || {}) && (params.token = token);
  return axios.delete(`${ base }/api/z39audio`, { "params": params }).then(res => res.data);
};
